package com.action;

import lombok.Data;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Data
public class UploadAction{
    private String name;
    private File face;
    private String faceFileName;
    private String faceContentType;
    public String execute() throws IOException {
        if(!faceFileName.endsWith(".jpg")){
            ServletActionContext.getContext().put("msg","文件格式错误！应上传jpg格式文件");
            return "err";
        }


        System.out.println(faceFileName);
        System.out.println(faceContentType);
        System.out.println(name);
        System.out.println(face.length());
        SimpleDateFormat sdf = new SimpleDateFormat("yyMM");
        String path = ServletActionContext.getServletContext().getRealPath("/upload/"+sdf.format(new Date()));
        File f = new File(path);
        if(!f.exists()){
            f.mkdirs();
        }

        File newface = new File(path,faceFileName);
        FileUtils.copyFile(face,newface);


        return null;
    }


}
